home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / ROBOTS3.ZIP / robots / robots2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  7.4 KB  |  233 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/timeb.h>
  4. #include "console.h"
  5.  
  6. unsigned Level, Peak, Bombs, dB;
  7. int Row, Col;
  8. unsigned Reds, Yellows;
  9.  
  10. byte Image[40][60], Im[40][60];
  11. int
  12.    dR[9] = { -1, 0, +1, -1, 0, +1, -1, 0, +1 },
  13.    dC[9] = { -1, -1, -1, 0, 0, 0, +1, +1, +1 };
  14.  
  15. void NewLevel(int dL) {
  16.    long L;
  17.    if (dL > 0) {
  18.       L = Level + ((0x4000L + (0x8000L - (long)Level)*dL) >> 15);
  19.    } else {
  20.       L = Level - ((0x4000L + (long)Level*(-dL)) >> 15);
  21.    }
  22.    if (L == Level) return;
  23.    Level = (unsigned)L, PutString(28, 32, WhiteC, "%5u", Level);
  24.    if (Level >= Peak)
  25.       Peak = Level, PutString(27, 32, WhiteC, "%5u", Peak);
  26. }
  27.  
  28. void SetRobot(void) {
  29.    struct timeb T;
  30.    ftime(&T); srand((unsigned int)(T.time ^ T.millitm)&0xffff);
  31.    Peak = Level = 100, Bombs = 1; dB = 0;
  32.    ScrInit();
  33. }
  34.  
  35. int SafeColor(int Row, int Col) {
  36.    int I, J, R, C, R1, C1;
  37.    if (Row < 0 || Row >= 40 || Col < 0 || Col >= 60) return BlackC;
  38.    for (I = 0; I < 9; I++) {
  39.       R = Row + dR[I], C = Col + dC[I];
  40.       if (R < 0 || R >= 40 || C < 0 || C >= 60) continue;
  41.       switch (Image[R][C]) {
  42.          case YellowC: case RedC: return BrownC;
  43.          case BrownC: continue;
  44.       }
  45.       for (J = 0; J < 9; J++) {
  46.          R1 = R + dR[J], C1 = C + dC[J];
  47.          if (R1 < 0 || R1 >= 40 || C1 < 0 || C1 >= 60) continue;
  48.          switch (Image[R1][C1]) {
  49.             case RedC: return BrownC;
  50.          }
  51.       }
  52.    }
  53.    return CyanC;
  54. }
  55.  
  56. void Show(int Row, int Col, int Hue) {
  57.    unsigned R = 10*(Row + 1), C = 10*(Col + 2);
  58.    Box(R + 1, R + 8, C + 1, C + 8, Hue);
  59.    Image[Row][Col] = Hue;
  60. }
  61.  
  62. void SafeKeys(void) {
  63.    PutString(27, 70, SafeColor(Row - 1, Col - 1), "Y");
  64.    PutString(27, 72, SafeColor(Row - 1, Col), "U");
  65.    PutString(27, 74, SafeColor(Row - 1, Col + 1), "I");
  66.    PutString(28, 70, SafeColor(Row, Col - 1), "H");
  67.    PutString(28, 72, SafeColor(Row, Col), "W");
  68.    PutString(28, 74, SafeColor(Row, Col + 1), "K");
  69.    PutString(29, 70, SafeColor(Row + 1, Col - 1), "B");
  70.    PutString(29, 72, SafeColor(Row + 1, Col), "N");
  71.    PutString(29, 74, SafeColor(Row + 1, Col + 1), "M");
  72. }
  73.  
  74. void RobotScreen(main) {
  75.    int R, C;
  76.    Box(10, 409, 20, 619, BlackC);
  77.    Border(9, 410, 19, 620, BlueC);
  78.    Border(7, 412, 17, 622, GreenC);
  79.    Border(5, 414, 15, 624, YellowC);
  80.    Border(3, 416, 13, 626, RedC);
  81.    PutString(28, 5, RedC, "R");
  82.    PutString(28, 7, YellowC, "O");
  83.    PutString(28, 9, GreenC, "B");
  84.    PutString(28, 11, CyanC, "O");
  85.    PutString(28, 13, BlueC, "T");
  86.    PutString(28, 15, PinkC, "S");
  87.    PutString(27, 25, YellowC, "Peak:");
  88.    PutString(28, 25, YellowC, "Level:");
  89.    PutString(29, 25, YellowC, "Bombs:");
  90.    PutString(27, 32, WhiteC, "%5d", Peak);
  91.    PutString(28, 32, WhiteC, "%5d", Level);
  92.    PutString(29, 32, WhiteC, "%5d", Bombs);
  93.    Row = rand()%40, Col = rand()%60, Show(Row, Col, CyanC);
  94.    PutString(28, 55, RedC, "Safe Keys:");
  95.    Reds = Yellows = 0;
  96.    for (R = 0; R < 40; R++) for (C = 0; C < 60; C++) {
  97.       unsigned L;
  98.       if (R == Row && C == Col) continue;
  99.       L = rand();
  100.       if (L < Level >> 2) Show(R, C, RedC), Reds++;
  101.       else if (L < Level) Show(R, C, YellowC), Yellows++;
  102.       else Show(R, C, BlackC);
  103.    }
  104. }
  105.  
  106. main() {
  107.    int dR, dC, R, C, R1, C1, NoKey;
  108.    SetRobot();
  109. RESTART:
  110.    RobotScreen(); NoKey = 0;
  111. NEXT:
  112.    if (Reds == 0 && Yellows == 0) { dB += Level; goto COMPLETE; }
  113.    if (NoKey) goto UPDATE;
  114. KEY:
  115.    SafeKeys();
  116.    while (1) switch (Keyboard()) {
  117.       case 3: ScrReset(); exit(1);
  118.       case 'y': case 'Y': dR = -1, dC = -1; goto GO;
  119.       case 'u': case 'U': case Up: dR = -1, dC = 0; goto GO;
  120.       case 'i': case 'I': dR = -1, dC = +1; goto GO;
  121.       case 'h': case 'H': case Left: dR = 0, dC = -1; goto GO;
  122.       case 't':
  123.          Show(Row, Col, BlackC);
  124.          Row = rand()%40, Col = rand()%60;
  125.          if (Image[Row][Col] != BlackC && Image[Row][Col] != CyanC) {
  126.             Image[Row][Col] = WhiteC; goto CRASH;
  127.          }
  128.       case 'w': goto UPDATE;
  129.       case 'W': NoKey = 1; goto UPDATE;
  130.       case 'k': case 'K': case Right: dR = 0, dC = +1; goto GO;
  131.       case 'b': case 'B': dR = +1, dC = -1; goto GO;
  132.       case 'n': case 'N': case Down: dR = +1, dC = 0; goto GO;
  133.       case 'm': case 'M': dR = +1, dC = +1; goto GO;
  134.       case 'a': case 'A':
  135.          if (Bombs == 0) break;
  136.          NewLevel(-2048);
  137.          PutString(29, 32, WhiteC, "%5d", --Bombs);
  138.          for (R = Row - 1; R <= Row + 1; R++)
  139.          for (C = Col - 1; C <= Col + 1; C++) {
  140.             if (R < 0 || R >= 40 || C < 0 || C >= 60) continue;
  141.             switch (Image[R][C]) {
  142.                case RedC: Reds--; NewLevel(8); Show(R, C, BlackC); break;
  143.                case YellowC: Yellows--; NewLevel(4); Show(R, C, BlackC); break;
  144.             }
  145.          }
  146.       goto UPDATE;
  147.    }
  148. GO:
  149.    R = Row + dR, C = Col + dC;
  150.    if (R < 0 || R >= 40 || C < 0 || C >= 60) goto KEY;
  151.    if (Image[R][C] == BrownC) {
  152.       int R1 = R + dR, C1 = C + dC;
  153.       if (R1 < 0 || R1 >= 40 || C1 < 0 || C1 >= 60) goto KEY;
  154.       if (Image[R1][C1] != BlackC) goto KEY;
  155.       Show(R1, C1, BrownC);
  156.       Show(Row, Col, BlackC);
  157.       Row = R, Col = C; goto UPDATE;
  158.    } else {
  159.       Show(Row, Col, BlackC);
  160.       switch (Image[R][C]) {
  161.          case RedC: NewLevel(8); Show(R, C, WhiteC); goto CRASH;
  162.          case YellowC: NewLevel(4); Show(R, C, WhiteC); goto CRASH;
  163.          case BlackC: Row = R, Col = C; goto UPDATE;
  164.       }
  165.    }
  166. UPDATE:
  167.    Show(Row, Col, CyanC);
  168.    for (R = 0; R < 40; R++) for (C = 0; C < 60; C++) {
  169.       Im[R][C] = Image[R][C];
  170.       switch (Image[R][C]) {
  171.          case YellowC: case RedC: Show(R, C, BlackC); break;
  172.       }
  173.    }
  174.    for (R = 0; R < 40; R++) for (C = 0; C < 60; C++) {
  175.       int Hue = Im[R][C], Hue2;
  176.       if (Hue != RedC && Hue != YellowC) continue;
  177.       R1 = R + (R < Row? +1: R > Row? -1: 0);
  178.       C1 = C + (C < Col? +1: C > Col? -1: 0);
  179.       switch (Image[R1][C1]) {
  180.          case CyanC: Show(R1, C1, WhiteC); goto CRASH;
  181.          case RedC: Reds--; NewLevel(8); goto BROWN;
  182.          case YellowC: Yellows--; NewLevel(4);
  183.          case BrownC: BROWN:
  184.             Show(R1, C1, BrownC);
  185.             if (Hue == RedC) Reds--, NewLevel(8);
  186.             else Yellows--, NewLevel(4);
  187.          break;
  188.          case BlackC: Show(R1, C1, Hue); break;
  189.       }
  190.    }
  191.    if (Reds == 0) goto NEXT;
  192.    for (R = 0; R < 40; R++) for (C = 0; C < 60; C++) {
  193.       Im[R][C] = Image[R][C];
  194.       switch (Image[R][C]) {
  195.          case RedC: Show(R, C, BlackC); break;
  196.       }
  197.    }
  198.    for (R = 0; R < 40; R++) for (C = 0; C < 60; C++) {
  199.       int Hue = Im[R][C], Hue2;
  200.       if (Hue != RedC) continue;
  201.       R1 = R + (R < Row? +1: R > Row? -1: 0);
  202.       C1 = C + (C < Col? +1: C > Col? -1: 0);
  203.       switch (Image[R1][C1]) {
  204.          case CyanC: Show(R1, C1, WhiteC); goto CRASH;
  205.          case RedC: Reds--; NewLevel(8); goto BROWN2;
  206.          case YellowC: Yellows--; NewLevel(4);
  207.          case BrownC: BROWN2:
  208.             Show(R1, C1, BrownC); Reds--, NewLevel(8);
  209.          break;
  210.          case BlackC: Show(R1, C1, Hue); break;
  211.       }
  212.    }
  213. goto NEXT;
  214. CRASH:
  215.    NewLevel(-8192);
  216.    PutString(27, 55, WhiteC, "MUNCH!!!");
  217.    dB = 0;
  218. COMPLETE:
  219.    PutString(29, 55, WhiteC, "ENDED");
  220.    while (1) switch (Keyboard()) {
  221.       case 3: case 0x1b: ScrReset(); exit(0);
  222.       case '\r':
  223.          if (dB > 500) {
  224.             unsigned dd = dB/500;
  225.             Bombs += dd, dB -= 500*dd;
  226.             PutString(29, 32, WhiteC, "%5d", Bombs);
  227.          }
  228.          PutString(27, 55, BlackC, "MUNCH!!!");
  229.          PutString(29, 55, BlackC, "ENDED");
  230.       goto RESTART;
  231.    }
  232. }
  233.